<cfif Not IsDefined("session.id")>
		<cflocation url = "/ecada/" addToken = "no">
</cfif>
	
<cfif IsDefined("session.experiment")>
	<cfset experiment = session.experiment>
<cfelse>
	<cflocation url="/ecada/">
</cfif>


<cfif IsDefined("form.submit")>
			
			<cfset tempUID = CreateUUID()>
			
			<cfdirectory action = "create" directory = "/#ExpandPath('/ecada/files/')#/#tempUID#" mode = 777 >
	
			<!--- Upload zipped practice stimuli. --->
			<cffile action="UPLOAD"
				destination="#ExpandPath('/ecada/files/' & tempUID)#"
				nameconflict="MAKEUNIQUE"
				filefield="ImageZipFile"
				result="ZipFile">

			<!--- Unzip directory. --->
			<cfzip
				action="unzip"
				file="#ExpandPath('/ecada/files/' & tempUID)#/#ZipFile.ServerFile#"
				destination="#ExpandPath('/ecada/files/' & tempUID)#"
			/>
			
			<cffile action="delete" file="#ExpandPath('/ecada/files/' & tempUID)#/#ZipFile.ServerFile#">

			<!--- Get file listing for directory. --->
			<cfdirectory 
				directory = "#ExpandPath('/ecada/files/' & tempUID)#"
				action = "list"
				filter="*.png|*.jpg|*.jpeg|*.gif|*.bmp"
				name = "getFiles"
			/>
			
			<cfloop query="getFiles">

				<cfset folderUID = CreateUUID()>
	
				<cfdirectory action = "create" directory = "#ExpandPath('/ecada/files/')#/#folderUID#" mode = 777 >
			
				<cffile action="move"
					source="#directory#/#name#"
					destination="#ExpandPath('/ecada/files/' & folderUID)#/#name#"
					result="test">
					
				
				<cfquery datasource="exp" result="filestore">
					INSERT INTO filestore (filename, filepath, fileext, mimetype)
					VALUES ('#name#', '#folderUID#', '#listLast(name,".")#', '')
				</cfquery>
		
				<cfquery datasource="exp" result="Stimuli">
					INSERT INTO stim (CondID, fileID, StimuliTypeID, Title, Description)
					VALUES ( #CondID#, #filestore.GENERATED_KEY#, (SELECT id FROM stimulitype WHERE type='picture'), '#listFirst(name, ".")#', '' )
				</cfquery>
				
		</cfloop>
		
		<cfdirectory 
				directory = "#ExpandPath('/ecada/files/' & tempUID)#"
				action = "list"
				name = "getAllFiles"
			/>
			
			<cfloop query="getAllFiles">
				<cffile action="delete" file="#ExpandPath('/ecada/files/' & tempUID)#/#ZipFile.ServerFile#">
			</cfloop>
			
			 <cfdirectory action="delete" directory="#ExpandPath('/ecada/files/' & tempUID)#"> 
	<cflocation url="./../" addToken="no">
<cfelse>
	<cfinclude template= "/ecada/header.cfm">
	<cfoutput>
		<form method="post" enctype="multipart/form-data" action="index.cfm">
			<fieldset>
				<legend>
					Stimuli Selection
				</legend>
				<p>
					You can select a zip file here to upload. The zip file must contain only images; anything that is not in the correct format will be ignored and then deleted.The best formats for these images are as follows:
					</p>
						<ul>
							<li>PNG</li>
							<li>JPG/JPEG</li>
							<li>GIF</li>
						</ul>	
<p>
					BMP's are also acceptable, however they are usally very large in size and are not recomened. If you use BMP's please take note that they time they take to download to the participants computer will affect the timing results.
				</p>
				<input type="file" name="ImageZipFile" id="ImagezipFile"></input>
			</fieldset>
			<input type="hidden" name="CondID" id="CondID" value="#HTMLEditFormat(condID)#">
			<input type="submit" name="submit" id="submit">
		</form>
	</cfoutput>
	<cfinclude template= "/ecada/footer.cfm">
</cfif>



